<?php
class ObjectiveForm extends CFormModel {

	const TOPIC_CHILDREN = "select b.* 
from project_topic a, project_topic b, topic c 
where a.id = :id 
and   a.topic_id = c.parent_id 
and   c.id = b.topic_id 
and   b.project_id = :project";

	private $meeting = null;
	private $steps = array ();
	private $objectives = array ();
	private $toSave = array ();

	public function init() {
		$this->meeting = Meetings::model()->findByPk($_GET['id']);
		$this->steps = ProjectSelectedSteps::model()->findAll('project_id=:project_id', array (
			':project_id' => $this->meeting->project->id
		));
		$this->objectives = $this->meeting->objectives();
	}

	public function getProject() {
		return $this->meeting->project;
	}

	public function getProject_id() {
		return $this->meeting->project->id;
	}

	public function getId() {
		return $this->meeting->id;
	}

	public function getMeeting() {
		return $this->meeting;
	}

	public function getSteps() {
		return $this->steps;
	}

	public function getObjectives() {
		return $this->objectives;
	}

	public function rules() {
		return array ();
	}

	public function attributeLabels() {
		return array (
			'adviser_validation' => Yii::t('project', 'Adviser Validation'),
			'owner_validation' => Yii::t('project', 'Owner Validation'),
			'adviser_validation' => Yii::t('comment', 'Comments')
		);
	}

	public function setAttributes($value) {
		parent::setAttributes($value);
		if (is_array($value) && isset ($value['objectives'])) {
			foreach ($value['objectives'] as $id => $items) {
				$objective = $this->getObjective($id);
				$items['adviser'] = (!isset ($items['adviser']) ? Constants::FALSE : $items['adviser']); 
				$items['owner'] = (!isset ($items['owner']) ? Constants::FALSE : $items['owner']); 
				$items['comment'] = (!isset ($items['comment']) ? "" : $items['comment']); 
				$this->loadValidationData('adviser', $objective, $items['adviser']);
				$this->loadValidationData('owner', $objective, $items['owner']);
				$this->loadComment($objective, $items['comment']);
			}
		}
	}

	public function getObjective($id) {
		foreach ($this->objectives as $objective) {
			if ($objective->id == $id) {
				return $objective;
			}
		}

		return null;
	}

	public function save() {
		foreach ($this->toSave as $id => $topic) {
			if (!$topic->save()) {
				foreach ($topic->getErrors() as $id => $errors){
					foreach ($errors as $error)
						$this->addError($id.'_'.$topic->id, $error."(".$topic->$id.")");
				}
			}
		}
		return !$this->hasErrors();
	}

	protected function loadValidationData($id, $objective, $validated) {
		$validation = $id . '_validation';
		$validation_topic = $id . '_validation_topic';
		$validation_date = $id . '_validation_date';
		if ($validated != $objective-> $validation) {
			if ($validated) {
				$objective-> $validation = Constants::TRUE;
				$objective-> $validation_date = $this->meeting->meeting_date;
				$objective-> $validation_topic = null;
				$this->toSave[$objective->id] = $objective;
				$queue = new Queue();
				$queue->enqueue_all(ProjectTopic::model()->findAllBySql(self::TOPIC_CHILDREN, array (
					':id' => $objective->id,
					':project' => $this->meeting->project->id
				)));
				while (!$queue->is_empty()) {
					$t = $queue->dequeue();
					if ($t-> $validation != Constants::TRUE) {
						$t-> $validation = Constants::TRUE;
						$t-> $validation_date = $this->meeting->meeting_date;
						$t-> $validation_topic = $objective->id;
						$this->toSave[$t->id] = $t;
						$queue->enqueue_all(ProjectTopic::model()->findBySql(self::TOPIC_CHILDREN, array (
							':id' => $t->id,
							':project' => $this->meeting->project->id
						)));
					}
				}
			} else {
				$objective-> $validation = Constants::FALSE;
				$objective->validation_comments = "";
				$objective-> $validation_date = null;
				$objective-> $validation_topic = null;
				$this->toSave[$objective->id] = $objective;
				$queue = new Queue();
				$queue->enqueue_all(ProjectTopic::model()->findAllBySql(self::TOPIC_CHILDREN, array (
					':id' => $objective->id,
					':project' => $this->meeting->project->id
				)));
				while (!$queue->is_empty()) {
					$t = $queue->dequeue();
					if ($t-> $validation != Constants::FALSE && $t-> $validation_topic == $objective->id) {
						$t-> $validation = Constants::FALSE;
						$t-> $validation_date = null;
						$t-> $validation_topic = null;
						$t->validation_comments = "";
						$this->toSave[$t->id] = $t;
						$queue->enqueue_all(ProjectTopic::model()->findBySql(self::TOPIC_CHILDREN, array (
							':id' => $t->id,
							':project' => $this->meeting->project->id
						)));
					}
				}
			}
		}
	}

	protected function loadComment($objective, $comment) {
		if ($objective->validation_comments == null)
			$objective->validation_comments = "";
		if ($comment != $objective->validation_comments)
		{
			$queue = new Queue();
			$queue->enqueue_all(ProjectTopic::model()->findBySql(self::TOPIC_CHILDREN, array (
				':id' => $objective->id,
				':project' => $this->meeting->project->id
			)));
			while (!$queue->is_empty()) {
				$t = $queue->dequeue();
				if ($t->validation_comments == null)
					$t->validation_comments = "";
				if ($t->validation_comments == $objective->validation_comments) {
					$t->validation_comments = $comment;
					$this->toSave[$t->id] = $t;
					$queue->enqueue_all(ProjectTopic::model()->findBySql(self::TOPIC_CHILDREN, array (
						':id' => $t->id,
						':project' => $this->meeting->project->id
					)));
				}
			}
			$objective->validation_comments = $comment;
		}
	}

}
?>

